<?php

require_once('../simpletest/autorun.php');
require_once('../../system/core/Router.php');
require_once('../../system/core/Route.php');
require_once('../../system/exceptions/UrlNotFoundException.php');

class TestOfRouter extends UnitTestCase {

    function testIsRouterResolvesStaticRoute() {
        // Setup
        $GLOBALS['routes'] = array(
            new system\core\Route('/static/static/route', 'Static'),
        );
        $_SERVER['REQUEST_URI'] = '/static/static/route';

        // Test
        try {
            $route = system\core\Router::getRoute();
        } catch (\system\exceptions\UrlNotFoundException $e) {
            $route = null;
        }

        // Verify
        $this->assertNotNull($route);
    }

    function testRouterThrowsExceptionIfCantResolveStaticRoute() {
        // Setup
        $GLOBALS['routes'] = array(
            new system\core\Route('/static/static/route', 'Static'),
        );
        $_SERVER['REQUEST_URI'] = '/static/static/route/xpto';

        // Test
        try {
            $route = system\core\Router::getRoute();
        } catch (\system\exceptions\UrlNotFoundException $e) {
            $route = null;
        }

        // Verify
        $this->assertNull($route);
    }

    function testIsRouterResolvesDynamicRoute() {
        // Setup
        $GLOBALS['routes'] = array(
            new system\core\Route('/static/static/:id(\d+)|', 'Static'),
        );
        $_SERVER['REQUEST_URI'] = '/static/static/123';

        // Test
        try {
            $route = system\core\Router::getRoute();
        } catch (\system\exceptions\UrlNotFoundException $e) {
            $route = null;
        }

        // Verify
        $this->assertNotNull($route);
    }

    function testIsRouterResolvesDynamicRouteAndSetGETvars() {
        // Setup
        $GLOBALS['routes'] = array(
            new system\core\Route('/static/static/:id(\d+)/:login(\w+)/andFinalyStatic', 'Static'),
        );
        $_SERVER['REQUEST_URI'] = '/static/static/123/login/andFinalyStatic';

        // Test
        try {
            $route = system\core\Router::getRoute();
        } catch (\system\exceptions\UrlNotFoundException $e) {
            $route = null;
        }

        // Verify
        $this->assertNotNull($route);
        $this->assertTrue(isset($_GET['id']));
        $this->assertEqual($_GET['id'], 123);
        $this->assertTrue(isset($_GET['login']));
        $this->assertEqual($_GET['login'], 'login');
    }

}